package krasa.usefulactions.commons;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.IllegalStateException;import java.lang.String;import java.util.zip.GZIPOutputStream;

/** loads file, compresses it and deletes source */
public class GzipFilePostProcessor implements FilePostProcessor {

	public void process(File resourceFile) {
		GZIPOutputStream out = null;
		try {
			// Create the GZIP output stream
			String outFilename = resourceFile.getPath() + ".gz";
			try {
				out = new GZIPOutputStream(new FileOutputStream(outFilename));
			} catch (IOException e) {
				throw new IllegalStateException(e);
			}

			// Open the input file
			FileInputStream in = new FileInputStream(resourceFile);

			// Transfer bytes from the input file to the GZIP output stream
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			resourceFile.delete();
			// Complete the GZIP file
			out.finish();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				throw new IllegalStateException(e);
			}
		}

	}
}
